home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 8: LINUX Games / Linux Cubed Series 8 - LINUX Games.iso / games / x11 / strategy / xsok-1.000 / xsok-1 / xsok-1.01 / src / X-widget.c < prev    next >
C/C++ Source or Header  |  1995-11-03  |  7KB  |  202 lines

  1. /*****************************************************************************/
  2. /*                                         */
  3. /*                                         */
  4. /*    Xsok version 1.00 -- module X-widget.c                     */
  5. /*                                         */
  6. /*    The tableau widget for Xsok.                         */
  7. /*    written by Michael Bischoff (mbi@mo.math.nat.tu-bs.de)             */
  8. /*    November-1994                                 */
  9. /*    see COPYRIGHT.xsok for Copyright details                 */
  10. /*                                         */
  11. /*                                         */
  12. /*****************************************************************************/
  13. #include "X-sok.h"
  14. #include <X11/IntrinsicP.h>
  15. #include <X11/StringDefs.h>
  16. #include "TableauP.h"
  17.  
  18. static XtResource resources[] = {
  19. #define offset(field) XtOffsetOf(TableauRec, tableau.field)
  20.     /* {name, class, type, size, offset, default_type, default_addr}, */
  21.     {XtNruleset, XtCRuleset, XtRString,    sizeof(String),    offset(rules),    XtRString,    "Sokoban"},
  22.     {XtNlevel,   XtCLevel,   XtRInt,       sizeof(int),       offset(level),    XtRImmediate, (XtPointer)1},
  23.     {XtNusername,XtCUsername,XtRString,    sizeof(String),    offset(username), XtRString,    NULL},
  24.     {XtNxsokdir, XtCXsokdir, XtRString,    sizeof(String),    offset(xsokdir),  XtRString,    XSOKDIR},
  25.     {XtNxpmdir,  XtCXpmdir,  XtRString,    sizeof(String),    offset(xpmdir),   XtRString,    NULL},
  26.     {XtNsavedir, XtCSavedir, XtRString,    sizeof(String),    offset(savedir),  XtRString,    XSOKSAVE },
  27.     {XtNmessageFile,XtCMessageFile,XtRString,sizeof(String),  offset(messageFile), XtRString, "messages"},
  28.     {XtNkeyboardFile,XtCKeyboardFile,XtRString,sizeof(String),offset(keyboardFile),XtRString, "keys"},
  29. #undef offset
  30. };
  31.  
  32.  
  33. /* actions on the desktop area */
  34. /*ARGSUSED*/
  35. static void TableauKey(Widget w, XEvent *xev, String *s, Cardinal *num) {
  36.     key_press((XKeyPressedEvent *)xev);
  37. }
  38. /*ARGSUSED*/
  39. static void TableauBtn_down(Widget w, XEvent *xev, String *s, Cardinal *num) {
  40.     button_press((XButtonPressedEvent *)xev);
  41. }
  42. /*ARGSUSED*/
  43. static void Redisplay(Widget w, XEvent *xev, Region region) {
  44.     redraw_table((XExposeEvent *)xev);
  45. }
  46.  
  47. static XtActionsRec actions[] = {
  48.     /* {name, procedure}, */
  49.     { "tableau_k",    TableauKey },
  50.     { "tableau_d",    TableauBtn_down },
  51. };
  52.  
  53. static char translations[] = "\
  54. <Key>:        tableau_k()    \n\
  55. <BtnDown>:    tableau_d()    \n\
  56. ";
  57.  
  58.  
  59. static Boolean SetValues(Widget current, Widget request, Widget desired,
  60.              ArgList args, Cardinal *num_args) {
  61.     return FALSE;
  62. }
  63.  
  64. static void Initialize(Widget request, Widget xnew, ArgList args, Cardinal *n);
  65. static void Resize(Widget gw);
  66.  
  67.  
  68. static void Realize(Widget w, XtValueMask *valuemask, XSetWindowAttributes *winattr) {
  69.     *valuemask |= CWEventMask | CWBackingStore | CWBitGravity;
  70.     winattr->backing_store = WhenMapped;
  71.     winattr->bit_gravity = NorthWestGravity;
  72.     winattr->event_mask = KeyPressMask | ExposureMask | ButtonPressMask |
  73.             ButtonReleaseMask | StructureNotifyMask | Button3MotionMask;
  74.    (*(tableauClassRec.core_class.superclass)->core_class.realize)(w, valuemask, winattr);
  75. }
  76.  
  77. TableauClassRec tableauClassRec = {
  78.   { /* core fields */
  79.     /* superclass        */    (WidgetClass) &widgetClassRec,
  80.     /* class_name        */    "Tableau",
  81.     /* widget_size        */    sizeof(TableauRec),
  82.     /* class_initialize        */    NULL,
  83.     /* class_part_initialize    */    NULL,
  84.     /* class_inited        */    FALSE,
  85.     /* initialize        */    Initialize,
  86.     /* initialize_hook        */    NULL,
  87.     /* realize            */    Realize,
  88.     /* actions            */    actions,
  89.     /* num_actions        */    XtNumber(actions),
  90.     /* resources        */    resources,
  91.     /* num_resources        */    XtNumber(resources),
  92.     /* xrm_class        */    NULLQUARK,
  93.     /* compress_motion        */    TRUE,
  94.     /* compress_exposure    */    TRUE,
  95.     /* compress_enterleave    */    TRUE,
  96.     /* visible_interest        */    FALSE,
  97.     /* destroy            */    NULL,
  98.     /* resize            */    Resize,
  99.     /* expose            */    Redisplay,
  100.     /* set_values        */    SetValues,
  101.     /* set_values_hook        */    NULL,
  102.     /* set_values_almost    */    XtInheritSetValuesAlmost,
  103.     /* get_values_hook        */    NULL,
  104.     /* accept_focus        */    NULL,
  105.     /* version            */    XtVersion,
  106.     /* callback_private        */    NULL,
  107.     /* tm_table            */    translations,
  108.     /* query_geometry        */    XtInheritQueryGeometry,
  109.     /* display_accelerator    */    XtInheritDisplayAccelerator,
  110.     /* extension        */    NULL
  111.   },
  112.   { /* tableau fields */
  113.     /* empty            */    0
  114.   }
  115. };
  116.  
  117. WidgetClass tableauWidgetClass = (WidgetClass)&tableauClassRec;
  118.  
  119. static Widget toplev = NULL;
  120.  
  121. static void Resize(Widget gw) {
  122.     TableauWidget w = (TableauWidget) gw;
  123.     /* printf("Parent wants me to have size %d,%d\n",
  124.        w->core.width, w->core.height); */
  125.     resize_event(w->core.width, w->core.height);
  126.     /* (*pileWidgetClass->core_class.superclass->core_class.resize)(gw); */
  127. }
  128.  
  129. void AskWidgetForResize(XSize_t x, XSize_t y) {
  130.     XtWidgetGeometry Geo;
  131.     XtGeometryResult r;
  132.  
  133.     Geo.width = x;
  134.     Geo.height = y;
  135.     do {
  136.     Geo.request_mode = CWWidth | CWHeight;
  137. #ifdef LABER
  138.     printf("resize to %d %d yielded ", Geo.width, Geo.height);
  139. #endif
  140.     r = XtMakeGeometryRequest(toplev, &Geo, &Geo);
  141. #ifdef LABER
  142.     switch (r) {
  143.     case XtGeometryYes:    printf("YES!\n");break;
  144.     case XtGeometryNo:    printf("NO!\n"); break;
  145.     case XtGeometryAlmost:printf("Almost!\n"); break;
  146.     case XtGeometryDone:    printf("Done!\n"); break;
  147.     }
  148. #endif
  149.     } while (r == XtGeometryAlmost);
  150. }
  151.  
  152. const char *keyfilename;
  153.  
  154. static void Initialize(Widget request, Widget xnew, ArgList args, Cardinal *n) {
  155.     static int is_inited = 0;
  156.     TableauWidget new = (TableauWidget)xnew;
  157.     TableauPart *p = &new->tableau;
  158.     toplev = xnew;
  159.     if (is_inited) {
  160.     fprintf(stderr, "Sorry, currently only one instance of Tableau is allowed\n");
  161.     exit(EXIT_FAILURE);
  162.     }
  163.  
  164.     xsokdir = p->xsokdir;
  165.     savedir = p->savedir;
  166.     setlangdir();
  167.     if (!p->xpmdir)
  168.     p->xpmdir = p->xsokdir;
  169.     if (strlen(savedir) > MAXSAVEFILELEN-16 ||
  170.     strlen(xsokdir) > MAXXSOKDIRLEN ||
  171.     strlen(p->xpmdir) > MAXXSOKDIRLEN) {
  172.     fprintf(stderr, "directory too long\n");
  173.     exit(1);
  174.     }
  175.     if (strlen(p->keyboardFile) > MAXFILENAMELEN ||
  176.     strlen(p->messageFile) > MAXFILENAMELEN) {
  177.     fprintf(stderr, "filename too long\n");
  178.     exit(1);
  179.     }
  180.     keyfilename = p->keyboardFile;
  181.     read_message_file(p->messageFile);
  182.     read_keyboard_file(p->keyboardFile);
  183.  
  184.     /* assign global data for old Xlib program */
  185.     dpy    = XtDisplay(new);
  186.  
  187.     buildusername(p->username);
  188.     /* gamegraphic = 1; */
  189.     change_rules(p->rules);
  190.     NewLevel(p->level);
  191.  
  192.     init_gfx(p->xpmdir);        /* make GCs, read xpm files */
  193.     init_layout();
  194. #ifdef LABER
  195.     printf("Init widget: res %d grap %d\n", new->core.width, graphic.width);
  196. #endif
  197.     if (new->core.width < graphic.width)
  198.     new->core.width = graphic.width;
  199.     if (new->core.height < graphic.height)
  200.     new->core.height = graphic.height;
  201. }
  202.